<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目封面" prop="projectCover">
        <el-input v-model="queryParams.projectCover" placeholder="请输入项目封面" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="视频教程URL" prop="videoUrl">
        <el-input v-model="queryParams.videoUrl" placeholder="请输入视频教程URL" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="项目价格" prop="price">
        <el-input v-model="queryParams.price" placeholder="请输入项目价格" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="浏览量" prop="viewsCount">
        <el-input v-model="queryParams.viewsCount" placeholder="请输入浏览量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="在线地址" prop="projectUrl">
        <el-input v-model="queryParams.projectUrl" placeholder="请输入在线地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支持修改" prop="isUpdate">
        <el-input v-model="queryParams.isUpdate" placeholder="请输入支持修改" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支持部署" prop="isDeploy">
        <el-input v-model="queryParams.isDeploy" placeholder="请输入支持部署" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="支持讲解" prop="isTeach">
        <el-input v-model="queryParams.isTeach" placeholder="请输入支持讲解" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['blog:project:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['blog:project:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['blog:project:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['blog:project:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目ID" align="center" prop="projectId" />
      <el-table-column label="项目名称" align="center" prop="title" />
      <el-table-column label="项目封面" align="center" prop="projectCover" width="150">
        <template slot-scope="scope">
          <el-image :src="scope.row.projectCover" style="width: 100px;" fit="contain" />
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickName" width="150" />
      <el-table-column label="项目价格" align="center" prop="price" width="100" />
      <el-table-column label="浏览量" align="center" prop="viewsCount" width="80" />
      <el-table-column label="支持修改" align="center" prop="isUpdate" width="80">
        <template #header>
          <el-tooltip content="支持二次开发/修改/定制" placement="top">
            <span>支持修改</span>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          <el-icon v-if="scope.row.isUpdate === 1" class="el-icon-check"></el-icon>
          <el-icon v-else class="el-icon-close"></el-icon>
        </template>
      </el-table-column>
      <el-table-column label="支持部署" align="center" prop="isDeploy" width="80">
        <template #header>
          <el-tooltip content="支持远程部署/安装/调试" placement="top">
            <span>支持部署</span>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          <el-icon v-if="scope.row.isDeploy === 1" class="el-icon-check"></el-icon>
          <el-icon v-else class="el-icon-close"></el-icon>
        </template>
      </el-table-column>
      <el-table-column label="支持讲解" align="center" prop="isTeach" width="80">
        <template #header>
          <el-tooltip content="支持1v1讲解" placement="top">
            <span>支持讲解</span>
          </el-tooltip>
        </template>
        <template slot-scope="scope">
          <el-icon v-if="scope.row.isTeach === 1" class="el-icon-check"></el-icon>
          <el-icon v-else class="el-icon-close"></el-icon>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['blog:project:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['blog:project:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目封面" prop="projectCover">
          <el-input v-model="form.projectCover" placeholder="请输入项目封面" />
        </el-form-item>
        <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item>
        <el-form-item label="视频教程URL" prop="videoUrl">
          <el-input v-model="form.videoUrl" placeholder="请输入视频教程URL" />
        </el-form-item>
        <el-form-item label="项目价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入项目价格" />
        </el-form-item>
        <el-form-item label="浏览量" prop="viewsCount">
          <el-input v-model="form.viewsCount" placeholder="请输入浏览量" />
        </el-form-item>
        <el-form-item label="在线地址" prop="projectUrl">
          <el-input v-model="form.projectUrl" placeholder="请输入在线地址" />
        </el-form-item>
        <el-form-item label="支持修改" prop="isUpdate">
          <el-input v-model="form.isUpdate" placeholder="请输入支持修改" />
        </el-form-item>
        <el-form-item label="支持部署" prop="isDeploy">
          <el-input v-model="form.isDeploy" placeholder="请输入支持部署" />
        </el-form-item>
        <el-form-item label="支持讲解" prop="isTeach">
          <el-input v-model="form.isTeach" placeholder="请输入支持讲解" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject } from "@/api/blog/project"

export default {
  name: "blog-project-admin",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        introduce: null,
        projectCover: null,
        userId: null,
        videoUrl: null,
        price: null,
        viewsCount: null,
        projectUrl: null,
        isUpdate: null,
        isDeploy: null,
        isTeach: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询项目列表 */
    getList() {
      this.loading = true
      listProject(this.queryParams).then(response => {
        this.projectList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        projectId: null,
        title: null,
        introduce: null,
        projectCover: null,
        userId: null,
        videoUrl: null,
        price: null,
        viewsCount: null,
        projectUrl: null,
        isUpdate: null,
        isDeploy: null,
        isTeach: null,
        createTime: null,
        updateTime: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.projectId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.$router.push('/blog/admin/project/add')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const projectId = row.projectId || this.ids
      this.$router.push(`/blog/admin/project/edit/${projectId}`)
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.projectId != null) {
            updateProject(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addProject(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const projectIds = row.projectId || this.ids
      this.$modal.confirm('是否确认删除项目编号为"' + projectIds + '"的数据项？').then(function () {
        return delProject(projectIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => { })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('blog/project/export', {
        ...this.queryParams
      }, `project_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
